package com.shopping_center.management.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.MapUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shopping_center.management.common.DataWrapper;
import com.shopping_center.management.common.ResultCode;
import com.shopping_center.management.excel.DownloadData;
import com.shopping_center.management.model.Car;
import com.shopping_center.management.model.DropDownList;
import com.shopping_center.management.model.OptionBean;
import com.shopping_center.management.service.CarService;
import com.shopping_center.management.utils.GenerateUUID;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author YeFeng
 * @date 2023/2/2
 */
@RequestMapping("car")
@RestController
@CrossOrigin
@Api(tags = "Car")
@Slf4j
public class CarController {

    @Autowired
    private CarService carService;

    @PostMapping("/insertInToCarInfo")
    public DataWrapper<?> insertInToCarInfo(@Validated @RequestBody Car car) {
        carService.insertInToCarInfo(car);
        return DataWrapper.successOf(ResultCode.SAVE_CAR_INFO_SUCCESS);
    }

    @PostMapping("/selectAllCarInfo")
    public DataWrapper<?> selectAllCarInfo(int pageNum, int pageSize) {

        PageInfo<Car> cars = carService.selectAllCarInfo(pageNum, pageSize);
        return DataWrapper.successWithData(ResultCode.SELECT_ALL_CAR_INFO, cars);
    }

    @PostMapping("/editCarInfo")
    public DataWrapper<?> editCarInfo(@Validated @RequestBody Car car) {
        carService.editCarInfo(car);
        return DataWrapper.successOf(ResultCode.MODIFY_CAR_INFO_SUCCESS);
    }

    @PostMapping("/deleteCarInfoById")
    public DataWrapper<?> deleteCarInfoById(String carId) {
        carService.deleteCarInfoById(carId);
        return DataWrapper.successOf(ResultCode.DELETE_CAR_INFO_SUCCESS);
    }

    @GetMapping("/downloadCarInfo")
    public void downloadFailedUsingJson(HttpServletResponse response) throws IOException {
        List<Car> cars = carService.selectAllCarInfoIntoExecl();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("车辆信息", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), DownloadData.class).autoCloseStream(Boolean.FALSE).sheet("车辆信息")
                    .doWrite(cars);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = MapUtils.newHashMap();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(map);
        }
    }

    @PostMapping("/selectCarInfoByFilter")
    public DataWrapper<?> selectCarInfoByFilter(int pageNum, int pageSize, String carManufacturer, String carEnergyType, String carAppearanceColor) {

        PageInfo<Car> cars = carService.selectCarInfoByFilter(pageNum, pageSize, carManufacturer, carEnergyType, carAppearanceColor);
        return DataWrapper.successWithData(ResultCode.SELECT_ALL_CAR_INFO, cars);
    }

    @PostMapping("selectFilter")
    public DataWrapper<?> selectFilter() {
        DropDownList list = carService.selectAllCarInfo();
        return DataWrapper.successWithData(ResultCode.SELECT_FILTER_INFO, list);
    }
}

